package com.spboot.controller;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.spboot.entity.User;
import com.spboot.page.PageInfo;
import com.spboot.service.RoleService;
import com.spboot.service.UserService;

import freemarker.template.utility.StringUtil;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author guocb
 * @since 2019-12-25
 */
@Controller
@RequestMapping("/user")
public class UserController {

	@Autowired
	UserService userService;

	@Autowired
	RoleService roleService;
	
	//权限注解
	@RequiresPermissions("user_list")
	@RequestMapping("/list")
	public String list(@RequestParam(defaultValue = "1",required = false)Long pageno,String userName,
			Integer userRole,Model model) {
		
		//自定义分页
		
		//条件构造器
		QueryWrapper<User> queryWrapper = new QueryWrapper<>();
		if(StringUtils.isNotEmpty(userName)){
			queryWrapper.likeRight(User.USERNAME, userName);
		}
		if(userRole!=null && userRole!=0){
			queryWrapper.eq(User.USERROLE, userRole);
		}
		//设置使用该条件构造器的条件(仅查询时加)
		queryWrapper.eq("isDelete",0);
		//分页必须开启分页插件
		PageInfo<User> pageInfo = new PageInfo<>(pageno, 6);
		pageInfo = (PageInfo<User>) userService.selectPageUser(pageInfo,queryWrapper);
		
		model.addAttribute("pageInfo", pageInfo);
		model.addAttribute("roleList",roleService.list());
		return "userList";
	}

	@RequiresPermissions("user_toadd")
	@GetMapping("/useradd")
	public String useradd(Model model) {
		
		
		model.addAttribute("roleList", roleService.list());
		return "useradd";
	}

	@RequiresPermissions("user_add")
	@PostMapping("/add")
	public String add(User user,Model model) {
		
		//对密码进行处理
		String newPaswword = new SimpleHash("md5",user.getUserPassword(),user.getUserCode(),2).toString();
		user.setUserPassword(newPaswword);
		boolean f = userService.save(user);
		if(f){

			return "redirect:list";
		}

		model.addAttribute("roleList", roleService.list());
		return "useradd";
	}
	
	@RequiresPermissions("user_get")
	@RequestMapping("get")
	public String get(Long id,Model model){
		User user = userService.getById(id);
		model.addAttribute("user", user);

		model.addAttribute("roleList", roleService.list());
		return "useredit";
	}
	
	@RequiresPermissions("user_update")
	@PostMapping("/edit")
	public String edit(User user,Model model) {

		boolean f = userService.updateById(user);
		if(f){

			return "redirect:list";
		}

		return "redirect:get?id="+user.getId();
	}
	
	@RequiresPermissions("user_del")
	@RequestMapping("/delete")
	@ResponseBody
	public Boolean delete(User user){
//		QueryWrapper<User> wu=new QueryWrapper<>();
//		wu.eq(User.ID, user.getId());
		return userService.removeById(user);
	}
}
